package com.adobe.catalog.CustomComponent
{
	import Skin.ProgressBar.GearSkin;
	
	import com.adobe.catalog.qs.utils.MatrixTransformer;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Transform;
	import flash.geom.Vector3D;
	import flash.utils.Timer;
	
	import mx.core.UIComponent;
	
	import spark.components.Group;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.effects.Rotate3D;
	
	[Style(name="gearFillColor",type="uint", format="Color", inherit="no", theme="spark")]
	
	[Style(name="gearBorerColor",type="uint", format="Color", inherit="no", theme="spark")]
	
	public class AnimationGear extends SkinnableComponent
	{
		[SkinPart(required="false")]
		public var gear:UIComponent;
		
		private var timer:Timer;
		
		public function AnimationGear()
		{
			super();
			setStyle("skinClass",GearSkin);
			createTimer();
		}
		
//		private var _direction:String;
//		[Inspectable(type="String", enumeration="Clockwise,UntiClockwise",defaultValue="Clockwise")] 
//		public function get direction():String
//		{
//			return _direction;
//		}
		
		private var _angle:Number=10;
		public function get angle():Number
		{
			return _angle;
		}

		public function set angle(value:Number):void
		{
			if( _angle != value )
			{
				_angle = value;
				if( timer.running )
				{
					timer.stop();
					timer.start();
				}
			}
			
		}
		
//		
//		public function set direction(value:String):void
//		{
//			_direction = value;
//		}

		
		override protected function createChildren():void
		{
			super.createChildren();
			
		}
		
		public function run():void
		{
			timer.start();
		}
		
		public function stop():void
		{
			timer.stop();
		}
		
		private var _gearSize:Number = 50;
		
		public function get gearSize():Number
		{
			return _gearSize;
		}
		[Bindable]
		public function set gearSize(value:Number):void
		{
			_gearSize = value;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList( unscaledWidth, unscaledHeight);
			gear.setStyle("fillColor",getStyle("gearFillColor"));
			gear.setStyle("borderColor",getStyle("gearBorerColor"));
		}
				
		private var position3D:Vector3D = new Vector3D();
		
		private function changeTransformCenter():void
		{
			if( !gear )
				return;
			var w:Number = gear.width;
			var h:Number = gear.height;
			var matrix:Matrix = gear.transform.matrix.clone();
			MatrixTransformer.rotateAroundInternalPoint(matrix,new Point(w/2,h/2),angle);
			gear.transform.matrix = matrix;
		}
		
		private function createTimer():void
		{
			timer = new Timer(100,1000);
			timer.addEventListener(TimerEvent.TIMER,onTimerHandler );
		}
		
		private function onTimerHandler( event:Event ):void
		{
			changeTransformCenter();
		}
	}
}